package com.jetinno.parts

import android.text.TextUtils
import android.util.Log
import com.jetinno.parts.ESNames.Companion.BTC
import com.jetinno.parts.ESNames.Companion.ES
import com.jetinno.parts.ESNames.Companion.FB
import com.jetinno.parts.ESNames.Companion.IN
import com.jetinno.parts.ESNames.Companion.LC
import com.jetinno.parts.ESNames.Companion.TS
import com.jetinno.parts.ESNames.Companion._2ES
import com.jetinno.parts.ESNames.Companion._2FB
import com.jetinno.parts.ESNames.Companion._2TS
import com.jetinno.parts.MilkNames.Companion.FM
import com.jetinno.parts.MilkNames.Companion.FV
import java.util.*

/**
 * Created by zhangyuncai.
 * Date: 2022/9/7
 * 零件类型
 */
object PartsHelper {
    private const val TAG = "PartsHelper"

    //特殊命名的机型
    private const val C = "C" //表示料盒，前面的数字表示数量，其中料盒数量包含豆盒（例如：ESFB7C表示1个豆盒+6个粉料盒）
    private const val B = "B" //B表示BIB袋，前面的数字表示数量，（果汁，鲜奶，凉茶等液体原料）+蠕动泵组合，多少个BIB表示为xB
    private const val S = "S" //S表示糖浆电机，多少个糖浆电机表示为xS
    private const val _Y = "_Y" //Y表示在原配置上增加自动压杯盖功能，例如JL500Y或者JL500_Y
    private const val IM = "_IM" //外置制冰机模组（注：内置制冰机将"I"放在型号之后,例如JL500I）

    @JvmStatic
    var globalParts: Map<String, Boolean>? = null

    @JvmStatic
    var partsInfo: PartsInfo = PartsInfo("JL300_ES_7C")


    /**
     * 设置机器信息
     * @param machineModel 机器类型 例如 "JL300_ES_Y_I_4C"
     * @param globalParts 机器零部件详情表 优先级高于machineModel,如果当前APP版本判断失误时,用该字段弥补时用
     */
    @JvmStatic
    fun setGlobal(machineModelInput: String, globalParts: Map<String, Boolean>) {
        try {
            var machineModel = machineModelInput!!.toUpperCase()
            machineModel = machineModel!!.replace("-".toRegex(), "_")
            PartsHelper.globalParts = globalParts
            partsInfo = PartsInfo(machineModel)

            //台机/柜机判断
            //JL50-ESLC_4C
            val machineSplits = machineModel.split("_".toRegex()).toTypedArray()
            var machineName = "";//机器名称,例如JL300
            var binNums = 0 //速溶料盒+咖啡豆盒的数量
            var bibNums = 0 //bib的数量
            var instantNums = 0 //速溶料盒数量
            var hasJLY = false //Y表示在原配置上增加自动压杯盖功能，例如JL500Y或者JL500_Y
            var hasJLI = false //I表示在原配置上增加内置制冰模组；例如JL500I
            var hasJLIM = false //外置制冰机模组（注：内置制冰机将"I"放在型号之后,例如JL500I）
            var hasCooler = false//是否有冷水机
            var hasIW = false//单独出水传感器//由开发者独创命名Independent of the water
            var isInstantMachine = false//速溶机
            var esName = ""//酿造器/泡茶器类型
            var syrupNums = 0//糖浆电机数量
            if (machineSplits != null) {
                for (machineSplit in machineSplits) {
                    if (machineSplit.contains("JL")) {
                        machineName = machineSplit
                        val chars = machineName.toCharArray()
                        for (aChar in chars) {
                            if (aChar == 'Y') {
                                hasJLY = true
                            }
                            if (aChar == 'I') {
                                hasJLI = true
                            }
                        }
                    }
                    if (machineSplit.length == 2 && machineSplit.contains("C")) { //JL50_ESLC_4C,推荐这种
                        //速溶料盒+咖啡豆盒的数量
                        val s0 = machineSplit[0]
                        if (s0 in '0'..'9') {
                            binNums = s0 - '0'
                        }
                    }
                    if (machineSplit.length == 2 && machineSplit.contains(B)) { //_4B,表示4个BIB
                        val s0 = machineSplit[0]
                        if (s0 in '0'..'9') {
                            bibNums = s0 - '0'
                        }
                    }
                    if (machineSplit.length == 2 && machineSplit.contains(S)) { //_4S,表示4个糖浆电机
                        val s0 = machineSplit[0]
                        if (s0 in '0'..'9') {
                            syrupNums = s0 - '0'
                        }
                    }
                    if (machineSplit.endsWith("C") && machineSplit.length > 3) { //JL50-ESLC4C
                        //速溶料盒+咖啡豆盒的数量
                        binNums = machineSplit[machineSplit.length - 2] - '0'
                    }
                    if (TextUtils.equals("I", machineSplit)) {
                        hasCooler = true
                    }
                    if (TextUtils.equals("IW", machineSplit)) {
                        hasIW = true
                    }
                    if (machineSplit.contains(IN)) {//_IN8C  速溶机 8个料盒
                        isInstantMachine = true
                        if (machineSplit.length == 4) {
                            val chars = machineSplit.toCharArray()
                            for (aChar in chars) {
                                val aInt = aChar - '0'
                                if (aInt in 0..9) {
                                    binNums = aInt
                                    break
                                }
                            }
                        }
                    }
                    if (TextUtils.isEmpty(esName)) {
                        for (name in ESNames.esNameList) {
                            if (TextUtils.equals(name, machineSplit)) {
                                esName = machineSplit
                            }
                        }
                    }
                }
            }
            if (!MachineNames.allList.contains(machineName)) {//必须是支持的机型
                for (s in MachineNames.allList) {
                    if (machineName.contains(s)) {
                        machineName = s
                        break
                    }
                }
            }
            if (!MachineNames.allList.contains(machineName)) {//必须是支持的机型
                //如果机型列表没有该机型,则给一个默认值
                machineName = MachineNames.JL300
            }

            var brewerNums = 0 //酿造器数量
            var teabrewerNums = 0 //泡茶器数量
            var grinderNums = 0 //研磨器数量
            var beanboxNums = 0 //豆盒数量

            if (machineModel.contains(_2ES)) { //2个豆盒+2个研磨器+1个酿造器
                brewerNums += 1
                grinderNums += 2
                beanboxNums += 2
            } else if (machineModel.contains(ES)) { //1个豆盒+1个研磨器+1个酿造器
                brewerNums += 1
                grinderNums += 1
                beanboxNums += 1
            }
            if (machineModel.contains(LC)) { //1个豆盒+1个研磨器+1个酿造器
                brewerNums += 1
                grinderNums += 1
                beanboxNums += 1
            }
            if (machineModel.contains(BTC)) { //1个豆盒+1个研磨器+1个泡茶器
                grinderNums += 1
                teabrewerNums += 1
                beanboxNums += 1
            }
            if (machineModel.contains(_2TS)) { //2个料盒+2个酿造器
                brewerNums += 2
            } else if (machineModel.contains(TS)) { //1个料盒+1个酿造器
                brewerNums += 1
            }
            if (machineModel.contains(_2FB)) { //2个料盒+2个泡茶器
                teabrewerNums += 2
            } else if (machineModel.contains(FB)) { //1个料盒+1个泡茶器
                teabrewerNums += 1
            }
            if (beanboxNums > 2) { //最多有两个咖啡豆盒
                beanboxNums = 2
            }
            if (binNums >= beanboxNums) {
                //速溶料盒=总料盒-咖啡豆料盒
                instantNums = binNums - beanboxNums
            }

            if (!hasJLY) {
                if (machineModel.contains(_Y)) {
                    hasJLY = true
                }
            }

            if (machineModel.contains(FM)) { //带奶泵冷热打奶泡功能
                partsInfo.milkName = FM
            }
            if (machineModel.contains(FV)) { //带文丘里打奶泡功能（只能打热奶泡）
                partsInfo.milkName = FV
            }
            if (machineModel.contains(IM)) { //外置制冰机模组（注：内置制冰机将"I"放在型号之后,例如JL500I）
                hasJLIM = true
            }

            partsInfo.machineName = machineName
            partsInfo.esName = esName
            partsInfo.hasJLY = hasJLY
            partsInfo.hasJLI = hasJLI
            partsInfo.hasJLIM = hasJLIM
            partsInfo.hasCooler = hasCooler
            partsInfo.hasIW = hasIW
            partsInfo.binNums = binNums
            partsInfo.beanboxNums = beanboxNums
            partsInfo.bibNums = bibNums
            partsInfo.instantNums = instantNums
            partsInfo.brewerNums = brewerNums
            partsInfo.teabrewerNums = teabrewerNums
            partsInfo.grinderNums = grinderNums
            partsInfo.isInstantMachine = isInstantMachine
            partsInfo.syrupNums = syrupNums


        } catch (e: Exception) {
            e.printStackTrace()
        }

        printAllParts();
    }


    @JvmStatic
    fun hasPartsByNum(num: Int): Boolean {
        //无论怎么配置,一定有
        if (num == PartIds.parts_flowmeter1 || num == PartIds.parts_all) {//流量计1
            return true
        }
        //一定有或者没有
        if (globalParts != null && globalParts!!.containsKey("$num")) {
            return globalParts!!.get("$num") ?: false
        }

        val machineName = partsInfo.machineName
        val has = when (num) {
            //单独空气泵阀
            PartIds.parts_tea_air_valve,
                //探杯传感器
            PartIds.parts_cupsensor -> true
            //酿造器
            PartIds.parts_brewer,
            PartIds.parts_brewer1 -> partsInfo.brewerNums > 0
            PartIds.parts_brewer2 -> partsInfo.brewerNums > 1
            //泡茶器
            PartIds.parts_teabrewer,
            PartIds.parts_teabrewer1 -> partsInfo.teabrewerNums > 0
            PartIds.parts_teabrewer2 -> partsInfo.teabrewerNums > 1
            //研磨器
            PartIds.parts_grinder1 -> partsInfo.grinderNums > 0
            PartIds.parts_grinder2 -> partsInfo.grinderNums > 1
            //豆盒
            PartIds.parts_beanbox,
            PartIds.parts_beanbox1 -> partsInfo.beanboxNums > 0
            PartIds.parts_beanbox2 -> partsInfo.beanboxNums > 1
            //搅拌电机数以料盒为准
            PartIds.parts_beater,
            PartIds.parts_beater1 -> partsInfo.instantNums > 0
            PartIds.parts_beater2 -> partsInfo.instantNums > 2
            PartIds.parts_beater3 -> partsInfo.instantNums > 4
            PartIds.parts_beater4 -> partsInfo.instantNums > 6
            PartIds.parts_beater5 -> partsInfo.instantNums > 8
            PartIds.parts_beater6 -> partsInfo.instantNums > 10
            //出料电机数以料盒为准
            PartIds.parts_discharge,
            PartIds.parts_discharge1 -> partsInfo.instantNums > 0
            PartIds.parts_discharge2 -> partsInfo.instantNums > 1
            PartIds.parts_discharge3 -> partsInfo.instantNums > 2
            PartIds.parts_discharge4 -> partsInfo.instantNums > 3
            PartIds.parts_discharge5 -> partsInfo.instantNums > 4
            PartIds.parts_discharge6 -> partsInfo.instantNums > 5
            PartIds.parts_discharge7 -> partsInfo.instantNums > 6
            PartIds.parts_discharge8 -> partsInfo.instantNums > 7
            //bib数量
            PartIds.parts_bib,
            PartIds.parts_bib1 -> partsInfo.bibNums > 0
            PartIds.parts_bib2 -> partsInfo.bibNums > 1
            PartIds.parts_bib3 -> partsInfo.bibNums > 2
            PartIds.parts_bib4 -> partsInfo.bibNums > 3
            PartIds.parts_bib5 -> partsInfo.bibNums > 4
            PartIds.parts_bib6 -> partsInfo.bibNums > 5
            PartIds.parts_bib7 -> partsInfo.bibNums > 6
            PartIds.parts_bib8 -> partsInfo.bibNums > 7
            //sryup数量
            PartIds.parts_syrup,
            PartIds.parts_syrup1 -> partsInfo.syrupNums > 0
            PartIds.parts_syrup2 -> partsInfo.syrupNums > 1
            PartIds.parts_syrup3 -> partsInfo.syrupNums > 2
            PartIds.parts_syrup4 -> partsInfo.syrupNums > 3
            PartIds.parts_syrup5 -> partsInfo.syrupNums > 4
            PartIds.parts_syrup6 -> partsInfo.syrupNums > 5
            //鲜奶机
            PartIds.parts_milk -> !TextUtils.isEmpty(partsInfo.milkName)
            //制冰机
            PartIds.parts_ice -> partsInfo.hasJLI || partsInfo.hasJLIM
            //压杯盖
            PartIds.parts_lid -> partsInfo.hasJLY
            //冷水机
            PartIds.parts_cold -> partsInfo.hasCooler
            //分杯系统
            PartIds.parts_cup,
                //小门
            PartIds.parts_smalldoor,
                //大门
            PartIds.parts_bigdoor -> (MachineNames.standList.contains(machineName) ||
                    TextUtils.equals(machineName, MachineNames.JL28))
            //单独出水传感器
            PartIds.parts_watersensor -> partsInfo.hasIW
            //滴水盘检测
            PartIds.parts_drip -> (MachineNames.tableList.contains(machineName))
            //紫外灯
            PartIds.parts_light_uv,
                //脏水桶水满检测
            PartIds.parts_addle -> (MachineNames.tableList.contains(machineName))
            //指示灯
            PartIds.parts_light_indicator -> (MachineNames.indicatorLightList.contains(machineName))
            //酿造器锅炉
            PartIds.parts_brewerboiler -> (MachineNames.JL852.contains(machineName))
            //(速溶机,且不含有酿造器),或者机型是JL22/JL24,则该机器用的不是齿轮泵
            PartIds.parts_gearpump -> (!((partsInfo.isInstantMachine && partsInfo.brewerNums == 0) ||
                    MachineNames.JL22.contains(machineName) ||
                    MachineNames.JL24.contains(machineName)
                    ))
            //接水盘,柜机专用
            PartIds.parts_light_indicator -> partsInfo.stand()
            //台机调速扇
            PartIds.parts_tablespeedfan -> partsInfo.table()
            //呼吸效果灯控制
            PartIds.parts_breathinglamp -> (MachineNames.JL300.contains(machineName) ||
                    MachineNames.JL28.contains(machineName)
                    )
            //机器运行灯控制
            PartIds.parts_machinerunninglamp -> (MachineNames.JL300.contains(machineName))
            else -> false
        }

        return has
    }

    /**
     * 是否是柜机
     *
     * @return
     */
    @JvmStatic
    fun isStandMachine(): Boolean = (partsInfo.stand())

    /**
     * 是否有废渣管理功能
     */
    @JvmStatic
    fun hasCoffeeGarbage(): Boolean {
        return !isStandMachine() && hasBrewer()
    }

    ///是否含有酿造器
    @JvmStatic
    fun hasBrewer(): Boolean {
        return hasPartsByNum(PartIds.parts_brewer)
    }

    ///是否含有酿造器1
    @JvmStatic
    fun hasBrewer1(): Boolean {
        return hasPartsByNum(PartIds.parts_brewer1)
    }

    ///是否含有酿造器2
    @JvmStatic
    fun hasBrewer2(): Boolean {
        return hasPartsByNum(PartIds.parts_brewer2)
    }

    ///是否含有泡茶器
    @JvmStatic
    fun hasTea(): Boolean {
        return hasPartsByNum(PartIds.parts_teabrewer)
    }

    ///是否含有泡茶器1
    @JvmStatic
    fun hasTea1(): Boolean {
        return hasPartsByNum(PartIds.parts_teabrewer1)
    }

    ///是否含有泡茶器2
    @JvmStatic
    fun hasTea2(): Boolean {
        return hasPartsByNum(PartIds.parts_teabrewer2)
    }

    ///是否含有出料电机
    @JvmStatic
    fun hasDischarge(): Boolean {
        return hasPartsByNum(PartIds.parts_discharge)
    }

    ///是否含有搅拌器
    @JvmStatic
    fun hasBeater(): Boolean {
        return hasPartsByNum(PartIds.parts_beater)
    }

    ///是否含有搅拌器1
    @JvmStatic
    fun hasBeater1(): Boolean {
        return hasPartsByNum(PartIds.parts_beater1)
    }

    ///是否含有搅拌器2
    @JvmStatic
    fun hasBeater2(): Boolean {
        return hasPartsByNum(PartIds.parts_beater2)
    }

    ///是否含有搅拌器3
    @JvmStatic
    fun hasBeater3(): Boolean {
        return hasPartsByNum(PartIds.parts_beater3)
    }

    ///是否含有搅拌器4
    @JvmStatic
    fun hasBeater4(): Boolean {
        return hasPartsByNum(PartIds.parts_beater4)
    }

    ///是否含有搅拌器5
    @JvmStatic
    fun hasBeater5(): Boolean {
        return hasPartsByNum(PartIds.parts_beater5)
    }

    ///是否含有搅拌器6
    @JvmStatic
    fun hasBeater6(): Boolean {
        return hasPartsByNum(PartIds.parts_beater6)
    }

    ///是否含有小门
    @JvmStatic
    fun hasSmalldoor(): Boolean {
        return hasPartsByNum(PartIds.parts_smalldoor)
    }

    ///是否含有大门
    @JvmStatic
    fun hasBigdoor(): Boolean {
        return hasPartsByNum(PartIds.parts_bigdoor)
    }

    ///是否含有分杯
    @JvmStatic
    fun hasCup(): Boolean {
        return hasPartsByNum(PartIds.parts_cup)
    }

    ///是否含有探杯传感器
    @JvmStatic
    fun hasCupSensor(): Boolean {
        return hasPartsByNum(PartIds.parts_cupsensor)
    }

    ///是否含有单独空气泵阀
    @JvmStatic
    fun hasTeaAirValve(): Boolean {
        return hasPartsByNum(PartIds.parts_tea_air_valve)
    }

    ///是否含有压盖
    @JvmStatic
    fun hasLid(): Boolean {
        return hasPartsByNum(PartIds.parts_lid)
    }

    ///是否含有制冰机
    @JvmStatic
    fun hasIce(): Boolean {
        return hasPartsByNum(PartIds.parts_ice)
    }

    ///是否含有奶泡机
    @JvmStatic
    fun hasMilk(): Boolean {
        return hasPartsByNum(PartIds.parts_milk)
    }

    ///是否含有BIB
    @JvmStatic
    fun hasBIB(): Boolean {
        return hasPartsByNum(PartIds.parts_bib)
    }

    ///是否含有咖啡豆盒
    @JvmStatic
    fun hasBeanBox(): Boolean {
        return hasPartsByNum(PartIds.parts_beanbox)
    }

    ///是否含有糖浆机
    @JvmStatic
    fun hasSyrup(): Boolean {
        return hasPartsByNum(PartIds.parts_syrup)
    }

    @JvmStatic
    fun hasColdWater(): Boolean {
        return partsInfo.machineName.contains("852") && partsInfo.hasJLI
    }

    /**
     * 打印该机器中含有的所有零部件的名称
     */
    private fun printAllParts() {
        Log.e(TAG, partsInfo.toString())
    }

}